﻿Imports System.Windows.Forms

Public Class MemberDialog

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        If txtmembername.Text = "" Then
            MsgBox("Invalid Member Name!", vbCritical)
            txtmembername.Focus()
            Return
        End If
        If txtsharedpath.Text = "" Then
            MsgBox("Invalid Path!", vbCritical)
            txtsharedpath.Focus()
        End If
        Me.DialogResult = System.Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub MemberDialog_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       
        LoadMembers()
    End Sub
    Sub LoadMembers()
        Try
            DataGridMembers.Refresh()
            DataGridMembers.DataSource = Nothing
            Dim conn As New SqlClient.SqlConnection
            With conn
                .ConnectionString = My.Settings.DataSource
                .Open()
            End With

            Dim cmd As New SqlClient.SqlCommand

            With cmd
                .CommandText = "SELECT [p_key],[company],[storename],[serverip] FROM [Management_Info_Sys].[dbo].[stores] where [serverip] like '1%';"
                .CommandType = CommandType.Text
                .CommandTimeout = 120
                .Connection = conn
            End With
            Dim dt As New DataTable
            dt.Load(cmd.ExecuteReader)

            With DataGridMembers
                .AutoGenerateColumns = True
                .DataSource = dt
            End With

            For Each item As DataGridViewTextBoxColumn In DataGridMembers.Columns
                item.AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

                If item.HeaderText = "company" Then
                    item.HeaderText = "Company"
                ElseIf item.HeaderText = "storename" Then
                    item.HeaderText = "Storename"
                ElseIf item.HeaderText = "serverip" Then
                    item.HeaderText = "Server IP"
                Else
                    item.Visible = False
                End If
            Next

            cmd.Dispose()
            cmd = Nothing
            conn.Dispose()
            conn = Nothing
        Catch ex As Exception
            MsgBox(ex.Message, vbCritical)
        End Try
    End Sub
    Private Sub DataGridMembers_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridMembers.CellContentClick
        Try
            Dim memberValue As String = DataGridMembers.Rows(e.RowIndex).Cells(1).Value & "_" & DataGridMembers.Rows(e.RowIndex).Cells(2).Value
            Dim Recommended As String = "\\" & DataGridMembers.Rows(e.RowIndex).Cells(3).Value & "\AlohaUpdates\"
            MemberID = DataGridMembers.Rows(e.RowIndex).Cells(0).Value
            txtmembername.Text = memberValue
            txtrecommended.Text = Recommended
            txtsharedpath.Text = Recommended
        Catch ex As Exception

        End Try
    End Sub
    Public MemberID As String
    Private Sub DataGridMembers_SelectionChanged(sender As Object, e As EventArgs) Handles DataGridMembers.SelectionChanged
        Try
            For Each selecteditems As DataGridViewRow In DataGridMembers.SelectedRows
                Dim memberValue As String = selecteditems.Cells(1).Value & "_" & selecteditems.Cells(2).Value
                Dim Recommended As String = "\\" & selecteditems.Cells(3).Value & "\AlohaUpdates\"
                MemberID = selecteditems.Cells(0).Value
                txtmembername.Text = memberValue
                txtrecommended.Text = Recommended
                txtsharedpath.Text = Recommended
                Exit For
            Next
        Catch ex As Exception

        End Try
    End Sub
End Class
